package util;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;

public class JsonUtils {
    public static void jsonWrite(HttpServletResponse resp,Object obj) {
        try {
            // 创建 Json 对象
            ObjectMapper om = new ObjectMapper();
            // 把数据对象转成 String (json格式)
            String json = om.writeValueAsString(obj);
            // 设置文件类型与字符集编码
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            // 输出到页面
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
